import React from 'react';
import {Tree} from 'antd';
import axios from 'axios';
import intercept from '../../utils/intercept';
import {config} from 'utils';
const TreeNode = Tree.TreeNode;

export default class Company extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
        organs:[],
        expandedKeys: ['41'],
        autoExpandParent: true,
        checkedKeys: ['41'],
        selectedKeys: [],
    };
  };
  onExpand = (expandedKeys) => {
    //console.log('onExpand', arguments);
    this.setState({
      expandedKeys,
      autoExpandParent: false,
    });
  }
  onCheck = (checkedKeys) => {
    //console.log('onCheck', checkedKeys);
    this.setState({ checkedKeys });
  }
  onSelect = (selectedKeys, info) => {
    //console.log('onSelect', info);
    this.setState({ selectedKeys });
  }
  componentDidMount() {
    this.getCompany();
  };
  getCompany = () => {
    const token = sessionStorage.getItem("token");
    axios.get(`${config.api}User/GetUserOrgans`, {
      headers: {
        Authorization: 'Bearer ' + token
      }
    }).then((res) => {
        //console.log(res.data);
        if(res.data.result){
            this.setState({
                organs:[res.data.userOrgans]
            })
        }
    })
    .catch(err => {
      intercept;
    })
  }
  renderTreeNodes = (data) => {
    return data.map((item) => {
      if (item.children != null) {
        return (
          <TreeNode title={item.userOrgan} key={item.organCode} dataRef={item}>
            {this.renderTreeNodes(item.children)}
          </TreeNode>
        );
      }
      return <TreeNode  title={item.userOrgan} key={item.organCode} dataRef={item}/>;
    });
  }
  render(){
      return(
        <Tree
        checkable
        onExpand={this.onExpand}
        expandedKeys={this.state.expandedKeys}
        autoExpandParent={this.state.autoExpandParent}
        onCheck={this.onCheck}
        checkedKeys={this.state.checkedKeys}
        onSelect={this.onSelect}
        draggable = {true}
        showLine = {true}
        selectedKeys={this.state.selectedKeys}
        style={{height:800,width:100}}
      >
        {this.renderTreeNodes(this.state.organs)}
      </Tree>
      )
  }
}